package com.py.androidUtils.UI;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.py.androidUtils.UI.view.BelongCategoryView;
import com.py.androidUtils.UI.view.BelongCategoryView.TypeOnClickListener;
import com.py.androidUtils.UI.view.RichEditor;
import com.py.androidUtils.UI.view.ShowPhotosView;
import com.py.androidUtils.UI.view.ShowPhotosView.PhotosClickListener;
import com.py.androidUtils.base.BaseActivity;
import com.py.androidUtils.utils.FileManagerUtils;
import com.py.androidUtils.utils.LogUtils;
import com.py.androidUtils.utils.ToastUtil;
import com.readyidu.app.R;

/**
 * @说明 富文本编辑
 * @作者 LY
 * @文件 RichTextReleasedActivity.java
 * @时间 2015年7月21日 下午6:13:02
 */
public class RichTextReleasedActivity extends BaseActivity implements OnClickListener {
	private TextView position;
	private TextView belongCategory;
	private RelativeLayout rlWeCanSee;
	private TextView canSeeType;
	private TextView invitationSee;
	private CheckBox circleFriends;
	private static String typeName = "";
	private String imgUrl = "";
	private ShowPhotosView showPhotosView;
	private RichEditor richEditor;
	private ImageView takingPictures;
	private TextView tvBold;
	private TextView tvSizeAdd;
	private int textSize = 18;
	private TextView tvSizeSubtract;
	private String htmlInfo = "";
	private EditText title;
	private String address = "";

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_rich_text_released);

		position = (TextView) this.findViewById(R.id.position);
		belongCategory = (TextView) this.findViewById(R.id.belongCategory);
		rlWeCanSee = (RelativeLayout) this.findViewById(R.id.rlWeCanSee);
		canSeeType = (TextView) this.findViewById(R.id.canSeeType);
		invitationSee = (TextView) this.findViewById(R.id.invitationSee);
		circleFriends = (CheckBox) this.findViewById(R.id.circleFriends);
		richEditor = (RichEditor) this.findViewById(R.id.editor);
		takingPictures = (ImageView) this.findViewById(R.id.taking_pictures);
		tvBold = (TextView) this.findViewById(R.id.tv_bold);
		tvSizeAdd = (TextView) this.findViewById(R.id.tv_size_add);
		tvSizeSubtract = (TextView) this.findViewById(R.id.tv_size_subtract);
		title = (EditText) this.findViewById(R.id.title);

		getTitleActionBar().setTitleBar("图片发布");
		getTitleActionBar().setImgLeft(R.drawable.icon_back, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finishactivity(RichTextReleasedActivity.this);
			}
		});
		getTitleActionBar().setTvRight1("发布", new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isCheckData()) {
					ToastUtil.showToast(getApplicationContext(), "标题：" + title.getText().toString() + "\n内容：" + htmlInfo + "\n地址：" + address + "\n类别：" + typeName + "\n可看：" + canSeeType.getText().toString() + "\n分享：" + (circleFriends.isChecked() ? "分享" : "不分享"));
				}
			}
		});

		richEditor.setEditorHeight(200);
		richEditor.setPlaceholder("文字");
		richEditor.setEditorFontSize(textSize);
		richEditor.setTextColor(getResources().getColor(R.color.text_hint));
		richEditor.setOnTextChangeListener(new RichEditor.OnTextChangeListener() {
			@Override
			public void onTextChange(String text) {
				LogUtils.i(text);
				htmlInfo = text;
			}
		});
		tvBold.setOnClickListener(this);
		tvSizeAdd.setOnClickListener(this);
		tvSizeSubtract.setOnClickListener(this);

		position.setOnClickListener(this);
		belongCategory.setOnClickListener(this);
		rlWeCanSee.setOnClickListener(this);
		invitationSee.setOnClickListener(this);
		takingPictures.setOnClickListener(this);

	}

	private boolean isCheckData() {
		if (title.getText().toString().trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请输入标题");
			return false;
		}
		if (htmlInfo.trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请输入文字");
			return false;
		}
		if (address.trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择地址");
			return false;
		}
		if (typeName.toString().trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择所属类型");
			return false;
		}
		if (canSeeType.getText().toString().trim().length() <= 0) {
			ToastUtil.showToast(getApplicationContext(), "请选择谁可看");
			return false;
		}
		return true;
	}

	private void addImages() {
		showPhotosView = new ShowPhotosView(RichTextReleasedActivity.this);
		showPhotosView.showView(position);
		showPhotosView.setPhotosClickListener(new PhotosClickListener() {
			@Override
			public void selectImages(int requestCode) {
			}

			@Override
			public void letCamera(String imagesPath, int requestCode) {
				imgUrl = imagesPath;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.position:
			launcherActivityForResult(RichTextReleasedActivity.this, PositionActivity.class, null, 1);
			break;
		case R.id.belongCategory:
			BelongCategoryView belongCategoryView = new BelongCategoryView(RichTextReleasedActivity.this);
			belongCategoryView.showDiaolge();
			belongCategoryView.setTypeOnClickListener(new TypeOnClickListener() {
				@Override
				public void getType(String type) {
					typeName = type;
					LogUtils.i(typeName);
				}
			});
			break;
		case R.id.rlWeCanSee:
			launcherActivityForResult(RichTextReleasedActivity.this, WeCanSeeActivity.class, null, 3);
			break;
		case R.id.addImages:
			break;
		case R.id.invitationSee:
			ToastUtil.showToast(getApplicationContext(), "邀请谁看");
			break;
		case R.id.taking_pictures:
			addImages();
			break;
		case R.id.tv_bold:
			richEditor.setBold();
			break;
		case R.id.tv_size_add:
			textSize++;
			richEditor.setEditorFontSize(textSize);
			break;
		case R.id.tv_size_subtract:
			if (textSize > 8) {
				textSize--;
				richEditor.setEditorFontSize(textSize);
			}
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg1 == RESULT_OK) {
			switch (arg0) {
			case 1:
				LogUtils.i(arg2.getStringExtra("name"));
				address = arg2.getStringExtra("name");
				break;
			case 2:
				LogUtils.i(arg2.getStringExtra("typeName"));
				typeName = arg2.getStringExtra("typeName");
				break;
			case 3:
				LogUtils.i(arg2.getStringExtra("type"));
				canSeeType.setText(arg2.getStringExtra("type"));
				break;
			case ShowPhotosView.IMAGES_ACTION_SELECT_IMAGES:// 选择照片
				imgUrl = FileManagerUtils.getInstance().getPath(RichTextReleasedActivity.this, arg2.getData());
				LogUtils.i("path" + imgUrl);
				richEditor.insertImage(imgUrl, "img");
				break;
			case ShowPhotosView.IMAGES_ACTION_CAMERA:// 拍照
				LogUtils.i("path" + imgUrl);
				richEditor.insertImage(imgUrl, "img");
				break;
			}
		}
	}
}
